<script setup lang="ts">
/* 
  1. 传入 show   orderId   fn   订单实付金额
*/
import { getConsultOrderPayUrl } from '@/services/consult'
import { ref } from 'vue'

const props = withDefaults(
  defineProps<{
    show: boolean
    orderId?: string
    onClose?: () => void
    actualPayment: number
    backUrl?: string
  }>(),
  {
    backUrl: '/room'
  }
)

const paymentMethod = ref<0 | 1>(1) // 微信支付 0  支付宝 1

const getUrl = async () => {
  if (!props.orderId) return

  const res = await getConsultOrderPayUrl({
    paymentMethod: 1,
    orderId: props.orderId,
    // http://localhost:5173   这是开发环境下的支付回跳地址的基础部分
    // http://localhost:4173   这是生产环境下的支付回跳地址的基础部分
    // payCallback: 'http://localhost:5173' + props.backUrl
    payCallback: import.meta.env.VITE_APP_CALLBACK_URL + props.backUrl
  })

  // console.log(res.data.payUrl) // 支付地址(支付宝写好一个收钱的页面)
  // 跳转自己项目的页面  router.push('/xxx')
  // 跳转外站   window.location.href=''
  window.location.href = res.data.payUrl // 通过支付地址打开支付宝 完成收钱
}

const emit = defineEmits<{
  (e: 'update:show', flag: boolean): void
}>()
</script>

<template>
  <van-action-sheet
    :show="show"
    @update:show="emit('update:show', $event)"
    title="选择支付方式"
    :close-on-popstate="false"
    :closeable="false"
    :before-close="onClose"
  >
    <div class="pay-type">
      <p class="amount">￥{{ actualPayment }}</p>
      <van-cell-group>
        <van-cell title="微信支付" @click="paymentMethod = 0">
          <template #icon><cp-icon name="consult-wechat" /></template>
          <template #extra>
            <van-checkbox :checked="paymentMethod === 0" />
          </template>
        </van-cell>
        <van-cell title="支付宝支付" @click="paymentMethod = 1">
          <template #icon><cp-icon name="consult-alipay" /></template>
          <template #extra>
            <van-checkbox :checked="paymentMethod === 1" />
          </template>
        </van-cell>
      </van-cell-group>

      <div class="btn">
        <van-button @click="getUrl" type="primary" round block>
          立即支付
        </van-button>
      </div>
    </div>
  </van-action-sheet>
</template>

<style scoped lang="scss">
.pay-type {
  .amount {
    padding: 20px;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
  }
  .btn {
    padding: 15px;
  }
  .van-cell {
    align-items: center;
    .cp-icon {
      margin-right: 10px;
      font-size: 18px;
    }
    .van-checkbox :deep(.van-checkbox__icon) {
      font-size: 16px;
    }
  }
}
</style>
